package com.example.androidhttpconnect;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;

public class AndroidHttpConnect extends Activity
{

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_android_http_connect);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_android_http_connect, menu);
		return true;
	}

	/**
	 * Async Tasks
	 */
	class LongRunningGetIO extends AsyncTask<Void, Void, String>
	{
		protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException
		{
			InputStream in = entity.getContent();
			StringBuffer out = new StringBuffer();
			int n = 1;
			while (n > 0)
			{
				byte[] b = new byte[4096];
				n = in.read(b);
				if (n > 0) out.append(new String(b, 0, n));
			}
			return out.toString();
		}

		@Override
		protected String doInBackground(Void... params)
		{
			HttpClient httpClient = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpGet httpGet = new HttpGet("URL REST EXAMPLE");
			String text = null;
			try
			{
				HttpResponse response = httpClient.execute(httpGet, localContext);
				HttpEntity entity = response.getEntity();
				text = getASCIIContentFromEntity(entity);
			}
			catch (Exception e)
			{
				return e.getLocalizedMessage();
			}
			return text;
		}

		protected void onPostExecute(String results)
		{
			if (results != null)
			{
				EditText et = (EditText) findViewById(R.id.my_edit);
				et.setText(results);
			}
			Button b = (Button) findViewById(R.id.my_button);
			b.setClickable(true);
		}
	}
}
